<?php

/*
	Class: DealersDealers

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>

	About: See Also
		- <Cts_Db_Table_Abstract>
*/
class DealersDealers extends Cts_Db_Table_Abstract {

	/* Group: Instance Variables */

	/*
		Variable: $_name
	*/
	protected $_name = 'dealers_dealers';

	/*
		Variable: $_primary
	*/
	protected $_primary = 'id';

	/* Group: Instance Methods */

	/*
		Function: get
			Retrieves one dealer from the database.

		Arguments:
			dealer_id - The ID of the dealer to retrieve.

		Returns: a dealer as an array, or an empty array
	*/
	function get($dealer_id) {
		$tmp_dealer = $this->fetchAll($this->select()->where("id = ?", $dealer_id));
		if (!is_null($tmp_dealer) && count($tmp_dealer) > 0) {
			$tmp_dealer = $tmp_dealer->toArray();
			return $tmp_dealer[0];
		} else {
			return array();
		}
	}

	/*
		Function: getAll
			Retrieves all dealers from the database with no filters.

		Returns: array of dealers or an empty array
	*/
	function getAll() {
		$tmp_dealers = $this->fetchAll();
		if (!is_null($tmp_dealers)) {
			return $tmp_dealers->toArray();
		} else {
			return array();
		}
	}

	/*
		Function: getByKeywords

		Arguments:
			keywords - A space-delimited list (string) of words to search for.
	*/
	function getByKeywords($keywords) {
		$keywords = split(" ", $keywords);
		$where = "1 = 1";
		foreach ($keywords as $word) {
			$where .= $this->getAdapter()->quoteInto(' and (name like ? or short_description like ?)', $word);
		}
		$tmp_dealers = $this->fetchAll($where);
		if (!is_null($tmp_dealers)) {
			return $tmp_dealers->toArray();
		} else {
			return array();
		}
	}

	/*
		Function: getBulk

		Arguments:
			ids - An array of IDs.

		Returns: array of row data or an empty array
	*/
	function getBulk(array $ids) {
		$where = "id in (0";
		foreach ($ids as $id) {
			$where .= ", ".$id;
		}
		$where .= ")";
		// $where should end up looking like this:
		// id in (0, 4, 23, 6, 324)
		$tmp_rows = $this->fetchAll($where);
		if (!is_null($tmp_rows)) {
			return $tmp_rows->toArray();
		} else {
			return array();
		}
	}

	/*
		Function: deleteBulk
			Deletes multiple dealers at once.

		Arguments:
			dealer_ids - An array of integers, the integers being dealer IDs.

		Returns: boolean
	*/
	function deleteBulk($ids) {
		$where = "id in ('0'";
		foreach ($ids as $id) {
			$where .= ", ".$id;
		}
		$where .= ")";
		// $where should end up looking like this:
		// id in (0, 4, 23, 6, 324)
		$rowsdeleted = $this->delete($where);
		if ($rowsdeleted >= 1) {
			return true;
		} else {
			return false;
		}
	}

	/*
		Function: deleteDependents
			Deletes all the category and country joins for a given dealer.
	*/
	function deleteDependents($dealer_id) {
		// TODO - this
	}
	
	function exists($id){
		if(count($this->getCountByWhereClause($this->getAdapter()->quoteInto("id = ?",$id)))){
			return true;
		} else {
			return false;
		}
	}

}
